package com.cskaoyan.controller.backgroundsystemcontroller;


import com.cskaoyan.bean.bo.AdListBo;
import com.cskaoyan.bean.bo.AdUpdateBo;
import com.cskaoyan.bean.pojo.Ad;
import com.cskaoyan.bean.vo.AdListVo;
import com.cskaoyan.bean.vo.AdUpdateVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author: lsy
 * @class: AdController
 * @description:
 * @creater: 刘思阳
 * @date: 2021/7/8 13:25
 */
@RestController
@RequestMapping("/admin/ad")
public class AdController {

    @Autowired
    PromotionService promotionService;

    /**
     * @author: lsy
     * @title: list
     * @description: 查询所有广告信息
     * @param: [adListBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("list")
    @RequiresPermissions(value = {"*","admin:ad:list"},logical = Logical.OR)
    public BaseRespVo list(AdListBo adListBo) {
        AdListVo adListVo = promotionService.queryAds(adListBo);
        return BaseRespVo.ok(adListVo);
    }

    /**
     * @author: lsy
     * @title: update
     * @description: 编辑广告内容
     * @param: [adUpdateBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("update")
    @RequiresPermissions(value = {"*","admin:ad:update"},logical = Logical.OR)
    public BaseRespVo update(@RequestBody @Validated AdUpdateBo adUpdateBo, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        AdUpdateVo adUpdateVo = promotionService.changeAd(adUpdateBo);
        return BaseRespVo.ok(adUpdateVo);
    }

    /**
     * @author: lsy
     * @title: delete
     * @description: 根据id删除广告
     * @param: [adUpdateBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("delete")
    @RequiresPermissions(value = {"*","admin:ad:delete"},logical = Logical.OR)
    public BaseRespVo delete(@RequestBody AdUpdateBo adUpdateBo) {
        AdUpdateBo adUpdateBo1 = new AdUpdateBo();
        adUpdateBo1.setId(adUpdateBo.getId());
        adUpdateBo1.setDeleted(true);
        promotionService.changeAd(adUpdateBo1);
        return BaseRespVo.ok();
    }

    @PostMapping("create")
    @RequiresPermissions(value = {"*","admin:ad:create"},logical = Logical.OR)
    public BaseRespVo create(@RequestBody @Validated Ad ad, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        Date date = new Date();
        ad.setAddTime(date);
        ad.setDeleted(false);
        ad.setUpdateTime(date);
        promotionService.addAd(ad);
        String url = ad.getUrl();
        File file = new File(url);
        return BaseRespVo.ok(ad);
    }
}
